package com.zoroki.student_score_system.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zoroki.student_score_system.domain.Score;
import com.zoroki.student_score_system.service.ScoreService;
import com.zoroki.student_score_system.mapper.ScoreMapper;
import com.zoroki.student_score_system.vo.ScoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【score】的数据库操作Service实现
* @createDate 2025-06-30 12:49:22
*/
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score>
    implements ScoreService{
    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    public List<Score> getScoreByStudentId(int studentId) {
        LambdaQueryChainWrapper<Score> eq = query().lambda().eq(Score::getStudentId, studentId);
        return scoreMapper.selectList(eq);
    }

    @Override
    public Score getScoreByStudentIdAndCourseId(int studentId, int courseId) {
        LambdaQueryChainWrapper<Score> eq = query().lambda().eq(Score::getStudentId, studentId).eq(Score::getCourseId, courseId);
        return scoreMapper.selectOne(eq);
    }

    @Override
    public boolean addScore(Score score) {
        return scoreMapper.insert(score) > 0;
    }

    @Override
    public boolean updateScore(Score score) {
        return scoreMapper.updateById(score) > 0;
    }

    @Override
    public boolean deleteScore(Integer scoreId) {
        return scoreMapper.deleteById(scoreId) > 0;
    }

    @Override
    public Page<ScoreVo> getAllScore(int pageNo, int pageSize, String msg, String type) {
        // 1. 构建分页条件：
        Page<ScoreVo> page = new Page<>(pageNo, pageSize);
        // 2. 创建对象
        Page<ScoreVo> allScore = null;
        // 3. 判断type类型：
        if(type.equals("学号")){
            allScore = scoreMapper.getScoreByUsername(page, msg);
        }else if(type.equals("姓名")){
            allScore = scoreMapper.getScoreByRealName(page, msg);
        }else if(type.equals("班级")){
            allScore = scoreMapper.getScoreByClassName(page, msg);
        }else if(type.equals("课程")){
            allScore = scoreMapper.getScoreByCourseName(page, msg);
        }else if(type.equals("教师")){
            allScore = scoreMapper.getScoreByTeacherName(page, msg);
        }else{
            allScore = scoreMapper.getAllScore(page);
        }
        return allScore;
    }
}




